import 'dart:ui' as ui;

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
/**
 * Create by laoge
 * on 2021/4/25 0025
 */

class ScalePainter extends CustomPainter {
  Paint linePaint = Paint()
    ..color = Colors.red
    ..strokeWidth = 1
    ..style = PaintingStyle.stroke;
  double smallScaleLineWidth = 1;
  Paint smallScaleLinePaint = Paint()
    ..color = Colors.white
    ..strokeWidth = 1
    ..style = PaintingStyle.stroke;
  double bigScalelineWidth = 2;
  Paint bigScaleLinePaint = Paint()
    ..color = Colors.white
    ..strokeWidth = 1
    ..style = PaintingStyle.fill;
  int lineInterval = 10;
  double min = 0, max = 100;
  double width = 200;
  double interval = 1;
  BuildContext mContext;
  double fontSize = 10;
  Color bgColor;
  Paint bgPaint = Paint();
  bool showTopLine;

  ScalePainter(this.mContext, this.width, this.lineInterval, this.min, this.max,
      this.interval,
      {this.fontSize = 10,
      this.bgColor = Colors.red,
      this.showTopLine = true}) {
    bigScaleLinePaint.strokeWidth = bigScalelineWidth;
    smallScaleLinePaint.strokeWidth = smallScaleLineWidth;
    bgPaint
      ..color = bgColor
      ..style = PaintingStyle.fill;
  }

  ui.ParagraphBuilder paragraphBuilder;
  double w;
  ui.Paragraph paragraph;

  @override
  void paint(Canvas canvas, Size size) {
    // print(size.width);
    // TODO: implement paint
    canvas.drawRect(
        Rect.fromPoints(Offset(0, 0), Offset(size.width, size.height)),
        bgPaint);
    for (int i = min ~/ interval; i <= (max - min) ~/ interval; i++) {
      if (i % 10 == 0) {
        paragraphBuilder = ui.ParagraphBuilder(
          ui.ParagraphStyle(
              textDirection: ui.TextDirection.ltr,
              fontSize: fontSize,
              textAlign: TextAlign.center),
        )
          ..pushStyle(ui.TextStyle(color: Colors.white))
          ..addText((i * interval).toString());
        w = calculateTextHeightAndWidth((i * interval).toString(), fontSize)
            .width;
        paragraph = paragraphBuilder.build()
          ..layout(ui.ParagraphConstraints(width: w));
        canvas.drawParagraph(paragraph,
            Offset(width / 2 + i * lineInterval - w / 2, size.height / 6));
      }
      if (i % 5 == 0) {
        canvas.drawLine(
            Offset(
                width / 2 + i * lineInterval,
                size.height / 6 +
                    calculateTextHeightAndWidth(
                            (i * interval).toString(), fontSize)
                        .height),
            Offset(width / 2 + i * lineInterval, size.height * 2 / 3),
            bigScaleLinePaint);
      } else {
        canvas.drawLine(
            Offset(
                width / 2 + i * lineInterval,
                size.height / 6 +
                    calculateTextHeightAndWidth(
                            (i * interval).toString(), fontSize)
                        .height),
            Offset(width / 2 + i * lineInterval,
                size.height * 2 / 3 - (size.height / 15)),
            smallScaleLinePaint);
      }
    }
    if (showTopLine) {
      canvas.drawLine(
          Offset(
              width / 2,
              size.height / 6 +
                  calculateTextHeightAndWidth(
                          (12 * interval).toString(), fontSize)
                      .height),
          Offset(
              size.width - width / 2,
              size.height / 6 +
                  calculateTextHeightAndWidth(
                          (12 * interval).toString(), fontSize)
                      .height),
          smallScaleLinePaint);
    }
  }

  @override
  bool shouldRepaint(ScalePainter oldDelegate) {
    // TODO: implement shouldRepaint
    return (oldDelegate.max != max ||
        oldDelegate.min != min ||
        oldDelegate.interval != interval ||
        oldDelegate.width != width ||
        oldDelegate.lineInterval != lineInterval);
  }

  ///value: 文本内容；fontSize : 文字的大小；fontWeight：文字权重；maxWidth：文本框的最大宽度；maxLines：文本支持最大多少行
  TextPainter calculateTextHeightAndWidth(String value, fontSize) {
    // value = filterText(value);
    TextPainter painter = TextPainter(

        ///AUTO：华为手机如果不指定locale的时候，该方法算出来的文字高度是比系统计算偏小的。
        locale: Localizations.localeOf(mContext),
        maxLines: 1,
        textDirection: TextDirection.ltr,
        text: TextSpan(
            text: value,
            style: TextStyle(
              fontWeight: FontWeight.normal,
              fontSize: fontSize,
            )));
    painter.layout(maxWidth: 1000);

    ///文字的宽度:painter.width
    return painter;
  }
}
